package com.hdoit.ares.web.brokerage.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.brokerage.model.BrokerageRecord;
import com.hdoit.ares.logical.brokerage.service.IBrokerageRecordService;
import com.hdoit.ares.logical.team.service.ICooperationTeamService;
import com.hdoit.ares.logical.viewmodel.brokerage.BrokerageRecordSearch;
import com.hdoit.ares.logical.viewmodel.cust.LoginRole;
import com.hdoit.ares.selfcommon.enums.acc.InsuranceType;
import com.hdoit.ares.selfcommon.utils.ExportExcel;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.export.excel.ExportModel;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年12月02日 14:29:31
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/brokerage/brokerageRecord/")
public class BrokerageRecordController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/brokerage/brokerageRecord/";
    }

    private IBrokerageRecordService brokerageRecordService;
    public IBrokerageRecordService getBrokerageRecordService() {
        return brokerageRecordService;
    }
    @Resource
    public void setBrokerageRecordService(IBrokerageRecordService brokerageRecordService) {
        this.brokerageRecordService = brokerageRecordService;
    }

    private IRoleEmployeeService roleEmployeeService;
	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}
	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}
	
	private ICooperationTeamService cooperationTeamService;
    public ICooperationTeamService getCooperationTeamService() {
		return cooperationTeamService;
	}
    @Resource
	public void setCooperationTeamService(ICooperationTeamService cooperationTeamService) {
		this.cooperationTeamService = cooperationTeamService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="brokerageRecordDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String exportExcelViewName = "exportExcel";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,BrokerageRecordSearch search,boolean ajaxPost){
    	String loginId = getLoginModel().getLoginId();
	    String roleId = roleEmployeeService.queryRoleIdsByEmployeeId(loginId).get(0);
        
	    if(LoginRole.HighMaker.equals(roleId)){
	    	search.setRefereeId(loginId);
	    }else if(LoginRole.CityMaker.equals(roleId)){
	    	search.setCityId(cooperationTeamService.queryModel(loginId).getCityId());
	    }
	    
	    pagerModel.setTotalAmount(brokerageRecordService.queryCount(search));
        List<BrokerageRecord> brokerageRecords= brokerageRecordService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("brokerageRecords", brokerageRecords);
        model.addAttribute("brokerageRecordSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = exportExcelViewName, method = { RequestMethod.GET,RequestMethod.POST })
	@ResponseBody
	public String exportExcel(BrokerageRecordSearch brokerageRecordSearch){
	    List<BrokerageRecord> brokerageRecords = brokerageRecordService.queryList(brokerageRecordSearch);
		
		ExportModel em=new ExportModel();
		
		
		em.setExcelInfoCode(ExportExcel.AllBrokerageRecordInfo);
		
		em.setDataSource(brokerageRecords);
		String cacheId=CommonUtils.getKeyId();
		cached.put(cacheId, em,60);
		
		return cacheId;
	}
    
    
    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String brokerageRecordDetailed(Model model,String key){
        BrokerageRecord brokerageRecord=new BrokerageRecord();
        if(!StringUtils.isEmpty(key)){
            brokerageRecord=brokerageRecordService.queryModel(key);
        }
        model.addAttribute("brokerageRecord", brokerageRecord);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,BrokerageRecord brokerageRecord){
        if(StringUtils.isEmpty(brokerageRecord.getId())){
            brokerageRecord.setId(CommonUtils.getKeyId());
            brokerageRecord.setRecordDate(DateUtils.getCurrent());
            brokerageRecord.setModelStatus(ModelStatus.add);
        }else{
            brokerageRecord.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(brokerageRecordService.applyChange(brokerageRecord,InsuranceType.NotCar)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("brokerageRecord", brokerageRecord);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(brokerageRecordService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}